﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		TagController.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Web.Mvc;
using Dexter.Logger.Contracts;
using Dexter.Search;
using Dexter.Services;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.ViewModel.FrontEnd.TagController;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.FrontEnd {
	[DexterCallFilter ( true )]
	public class TagController : PublicControllerBase {
		public TagController ( ISearchService searchService , ICommentService commentService , IReferrerService referrerService , IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IBlogRollService blogRollService , ICategoryService categoryService , IPostService postService , IThemeManager themeManager , ITagService tagService , IPageService pageService ) : base ( searchService , commentService , referrerService , configurationService , webUtils , logger , urlBuilder , blogRollService , categoryService , postService , themeManager , tagService , pageService ) {}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		public ActionResult Index ( ) {

			if(!base.Configuration.EnabledPages.EnableTagCloudRoute)
				return new HttpNotFoundResult ( );

			var data = base.GetViewData ( new TagIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );

			data.Tags = TagService.GetTopTags ( 500 );

			return View ( data );
		}

		[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
		public ActionResult Archive ( string id , int? page ) {
			if ( string.IsNullOrEmpty ( id ) ) {
				return HttpNotFound ( );
			}

			int pageIndex = page ?? 1;

			pageIndex--;

			var data = base.GetViewData ( new TagArchiveViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );

			data.FilterTag = id;
			data.Posts = TagService.GetPostDtoByTag(id, pageIndex, base.Configuration.ElementInListsConfiguration.NumberOfElementsInList);
			data.SetMetaKeywords ( base.GetKeywords ( data.Posts.Result ) );

			return View ( data );
		}

		public ActionResult LookupTags ( string tag ) {
			throw new NotImplementedException ( );
			//return Json ( TagService.GetTagsByInitial ( tag ).Select ( x => new { caption = x.EncodeHtml ( ), value = x.EncodeHtml ( ) } ) );
		}
	}
}
